﻿<%@ Application Language="C#" %>

<script RunAt="server">
    //时间间隔
    private int sleepInterval = 60000;//60秒
    private static string strUrl = "";
    void Application_Start(object sender, EventArgs e)
    {
        Kaidou.Util.LogManager.debug("===========Application_Start start ");
        //在应用程序启动时运行的代码
        System.Threading.ThreadStart myThreadStart = new System.Threading.ThreadStart(StartBatch);
        System.Threading.Thread schedulerThread = new System.Threading.Thread(myThreadStart);
        schedulerThread.Start();
    }
    
    void Application_BeginRequest(object sender, EventArgs e)
    {
        if (strUrl == "")
        {
            strUrl = HttpContext.Current.Request.Url.AbsoluteUri;
            Kaidou.Util.LogManager.debug("===========Application_BeginRequest start - start url:" + strUrl);
        }
    }
    
    
    void Application_End(object sender, EventArgs e)
    {
        // 在应用程序关闭时运行的代码
        //解决应用池回收问题
        System.Threading.Thread.Sleep(5000);
        Kaidou.Util.LogManager.debug("===========Application_End start - Request url:" + strUrl);
        try
        {
            System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);

            _HttpWebRequest.Method = "GET";

            System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)_HttpWebRequest.GetResponse();

            System.IO.Stream stream = httpWebResponse.GetResponseStream();

            System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);

            string strOut = streamReader.ReadToEnd();
            httpWebResponse.Close();
            stream.Close();
            streamReader.Close();
            Kaidou.Util.LogManager.debug("===========Application_End OK  ");
        }
        catch (Exception Ex)
        {
            Kaidou.Util.LogManager.debug("===========Application_End Error :" + Ex.ToString());
        }
        
    }

    void Application_Error(object sender, EventArgs e)
    {
        //在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e)
    {
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时，才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer，则不会引发该事件。

    }

    public void StartBatch()
    {
        bool BatchEndFlag = false;
        while (true)
        {
            if (System.DateTime.Now.Hour==0 && System.DateTime.Now.Minute > 30 && System.DateTime.Now.Minute < 35 )
            {
                if (!BatchEndFlag)
                {
                    Kaidou.Util.LogManager.debug("===========Batch Start  ");
                    Kaidou.Bll.ReceiptOrderStarter RO = new Kaidou.Bll.ReceiptOrderStarter(Resources.Resource.ResourceManager);
                    Kaidou.Bll.ReportRateStarter RR = new Kaidou.Bll.ReportRateStarter(Resources.Resource.ResourceManager);
                    BatchEndFlag = true;
                }
            }
            else
            {
                if (BatchEndFlag)
                {
                    BatchEndFlag = false;
                }
            }
            System.Threading.Thread.Sleep(sleepInterval);
        }
    }
           
</script>

